/// <remarks>
/// AppSettings Class
/// This class Load and Save data in the VcGUI.exe.config file
/// 
/// Authors: Georges Marceau, Matricis Informatique Inc.
///          Louis-Philippe Lebouthillier.
/// </remarks>

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using VcLib;
using System.IO;


namespace VcLib
{
   
    /// <summary>
    /// http://www.codeproject.com/useritems/SystemConfiguration.asp
    /// http://www.devasp.net/net/articles/display/679.html
    /// </summary>
    public class AppSettings 
    {
        Config config;

        public AppSettings()
        {
            this.config = Config.Instance;
            
            Load();
            
        }

        ~AppSettings()
        {
            // Save settings when instance is destroyed.
            Save();
        }

        // Write config
        public void Save()
        {
                // Open App.Config of executable
                System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration("path");
                
                // Add an Application Setting.
                config.AppSettings.Settings.Add("servername", this.config.ServerName);

            
                // Save the changes in appSetting.config file.
                config.Save(ConfigurationSaveMode.Modified);

                // Force a reload of a changed section.  
                ConfigurationManager.RefreshSection("appSettings");
        }

        // Read config
        public void Load()
        {
            try
            {
                config.ServerName = ConfigurationManager.AppSettings.Get("servername");
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
